package org.cxa.rpc.cloud.configurationConsole;

import javax.swing.JPanel;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.BoxLayout;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.border.TitledBorder;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.cxa.rpc.cloud.client.CloudClient.CloudClientListenerAdapter;
import org.cxa.rpc.cloud.client.TransientDevice;
import org.cxa.rpc.transportChannel.TransportChannel.DisconnectReason;


public class LoginPanel extends JPanel
{
	private static final long serialVersionUID = 2176675984365529823L;
	
	
	public interface LoginListener
	{
		public void onLoginSuccessful();
		public void onLogout(DisconnectReason reasonIn, boolean willAttemptReconnectIn);
	}
	
	
	private JTextField txtEmail;
	private JPasswordField txtPassword;
	private JButton btnLogin;

	private JLabel lblStatus;
	
	private List<LoginListener> listeners = new ArrayList<LoginListener>();
	
	
	private final TransientDevice tDev;
	
	
	/**
	 * Create the panel.
	 */
	public LoginPanel(TransientDevice tDevIn)
	{	
		// save our references and subscribe to tDev updates
		// if tDevIn is null, this may be WindowBuilder...play nice...
		this.tDev = tDevIn;
		if( tDevIn != null )
		{
			this.tDev.addListener(new CloudClientListenerAdapter()
			{
				@Override
				public void onConnect()
				{
					for( LoginListener currListener : listeners )
					{
						currListener.onLoginSuccessful();
					}
				}
				
				@Override
				public void onDisconnect(DisconnectReason reasonIn, boolean willAttemptReconnectIn)
				{
					for( LoginListener currListener : listeners )
					{
						currListener.onLogout(reasonIn, willAttemptReconnectIn);
					}
					lblStatus.setText(reasonIn.toString());
					lblStatus.setForeground(Color.RED);
					btnLogin.setEnabled(true);
				}
				
				@Override
				public void onFailedAuthentication(String errorIn)
				{
					lblStatus.setText(errorIn);
					lblStatus.setForeground(Color.RED);
					btnLogin.setEnabled(true);
				}
			});
		}
			
		setBorder(new TitledBorder(null, "Enter Login Credentials:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setMaximumSize(new Dimension(300, 126));
		setMinimumSize(new Dimension(300, 126));
		setPreferredSize(new Dimension(300, 126));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JPanel pnl_credentials = new JPanel();
		add(pnl_credentials);
		pnl_credentials.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,}));
		
		JLabel lblEmail = new JLabel("Email:");
		pnl_credentials.add(lblEmail, "2, 2, right, default");
		
		txtEmail = new JTextField();
		txtEmail.setText("root@localhost");
		pnl_credentials.add(txtEmail, "4, 2, fill, default");
		txtEmail.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		pnl_credentials.add(lblPassword, "2, 4, right, default");
		
		txtPassword = new JPasswordField();
		txtPassword.setText("password");
		pnl_credentials.add(txtPassword, "4, 4, fill, default");
		
		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("left:max(89dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.LINE_GAP_ROWSPEC,}));
		
		lblStatus = new JLabel("");
		panel.add(lblStatus, "2, 2");
		
		btnLogin = new JButton("Login");
		panel.add(btnLogin, "4, 2");
		btnLogin.addActionListener(new ActionListener()
		{
			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if( txtEmail.getText().isEmpty() )
				{
					lblStatus.setText("email adddress field must not be empty");
					lblStatus.setForeground(Color.RED);
					return;
				}
				else if( txtPassword.getText().isEmpty() ) 
				{
					lblStatus.setText("password field must not be empty");
					lblStatus.setForeground(Color.RED);
					return;
				}
				
				// set our new credentials
				tDev.setCredentials(txtEmail.getText(), txtPassword.getText());
				
				btnLogin.setEnabled(false);
				lblStatus.setText("Connecting...");
				lblStatus.setForeground(Color.BLACK);
				tDev.connect();
			}
		});
		this.btnLogin.requestFocus();
	}

	
	public void addLoginListener(LoginListener listenerIn)
	{
		if( listenerIn == null ) throw new IllegalArgumentException("listener must NOT be null");
		
		this.listeners.add(listenerIn);
	}
}
